package com.jwiki.app.jtoolkit.common.db;

public class Column {

    private String jdbcName;
    
    private String javaName;

    private boolean nullable = true;
    
    private boolean generated = false;

    private String remarks;

    private String defaults;

    private String jdbcType;

    private String javaType;
    
    public Column(String jdbcName) {
        this.jdbcName = jdbcName;
        this.javaName = getCamelName(jdbcName);
    }

    public String getJdbcName() {
        return jdbcName;
    }

    public String getJavaName() {
        return javaName;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public String getDefaults() {
        return defaults;
    }

    public void setDefaults(String defaults) {
        this.defaults = defaults;
    }

    public boolean isNullable() {
        return nullable;
    }

    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }

    public boolean isGenerated() {
        return generated;
    }

    public void setGenerated(boolean generated) {
        this.generated = generated;
    }

    public String getJdbcType() {
        return jdbcType;
    }

    public void setJdbcType(String jdbcType) {
        this.jdbcType = jdbcType;
        JdbcType eJdbcType;
        try {
            eJdbcType = JdbcType.valueOf(jdbcType);
        } catch (Exception e) {
            eJdbcType = RefType.valueOf(jdbcType).getJdbcType();
            this.jdbcType = eJdbcType.name();
        }
        this.javaType = eJdbcType.getJavaType();
    }

    public String getJavaType() {
        return javaType;
    }

    public void setJavaType(String javaType) {
        this.javaType = javaType;
    }
    
    protected String getCamelName(String name) {
        StringBuilder sb = new StringBuilder();
        boolean nextUpperCase = false;
        for (int i = 0; i < name.length(); i++) {
            char c = name.charAt(i);
            if (c == '_') {
                if (sb.length() > 0) {
                    nextUpperCase = true;
                }
            } else {
                if (nextUpperCase) {
                    sb.append(Character.toUpperCase(c));
                    nextUpperCase = false;
                } else {
                    sb.append(Character.toLowerCase(c));
                }
            }
        }
        return sb.toString();
    }

}
